package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Title: PathServlet
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 8:59
 */
@WebServlet("/path")
public class PathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取到项目的目录  项目的目录指向servlet 的web文件夹
        String contextPath = req.getContextPath();
        // 获取编译后的路径  out
        System.out.println(PathServlet.class.getResource("/").getPath());
        // 获取编译后的路径  getServletContext 获取servlet 上下文对象
        System.out.println(req.getServletContext().getRealPath("/WEB-INF/classes"));
        System.out.println(contextPath);
        // 重定向到index.html
        // resp.sendRedirect(contextPath+"/index.html");
        // 通过servlet 去访问
        resp.sendRedirect(contextPath+"/WEB-INF/success.html");
        //  resp.sendRedirect(req.getServletContext().getRealPath("/WEB-INF/success.html"));
        //resp.getWriter().write("success!");
    }
}
